@echo off
setlocal enabledelayedexpansion

:: ============================================
:: Flutter Web Startup Script (Windows)
:: ============================================

:: Enable ANSI color support (Windows 10+)
reg add HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1 /f >nul 2>&1

:: Define color codes
set "COLOR_RESET=[0m"
set "COLOR_RED=[91m"
set "COLOR_GREEN=[92m"
set "COLOR_YELLOW=[93m"
set "COLOR_BLUE=[94m"
set "COLOR_MAGENTA=[95m"
set "COLOR_CYAN=[96m"
set "COLOR_WHITE=[97m"
set "COLOR_BOLD=[1m"

cls
echo.
echo %COLOR_CYAN%========================================%COLOR_RESET%
echo %COLOR_BOLD%%COLOR_CYAN%   Flutter Web Startup Tool (Windows)%COLOR_RESET%
echo %COLOR_CYAN%========================================%COLOR_RESET%
echo.

:: Set working directory to script location
cd /d "%~dp0"

:: ============================================
:: Browser Selection
:: ============================================
echo %COLOR_YELLOW%Please select a browser:%COLOR_RESET%
echo   %COLOR_WHITE%1)%COLOR_RESET% Chrome %COLOR_GREEN%(Default)%COLOR_RESET%
echo   %COLOR_WHITE%2)%COLOR_RESET% Edge
echo.
echo %COLOR_YELLOW%Auto-select Chrome in 5 seconds if no input...%COLOR_RESET%
echo.

:: Use choice command for timeout selection
choice /C 12 /T 5 /D 1 /N /M "Enter number (1 or 2): "
set BROWSER_CHOICE=%ERRORLEVEL%

:: Set browser based on choice
if %BROWSER_CHOICE%==2 (
    set BROWSER=edge
    set BROWSER_NAME=Edge
) else (
    set BROWSER=chrome
    set BROWSER_NAME=Chrome
)

echo.
echo %COLOR_GREEN%Selected: !BROWSER_NAME!%COLOR_RESET%
echo.

:: ============================================
:: Configuration Parameters
:: ============================================
set WEB_PORT=8088
set WEB_HOSTNAME=127.0.0.1

echo %COLOR_CYAN%========================================%COLOR_RESET%
echo %COLOR_BOLD%%COLOR_CYAN%  Startup Configuration%COLOR_RESET%
echo %COLOR_CYAN%========================================%COLOR_RESET%
echo %COLOR_WHITE%Browser:%COLOR_RESET% %COLOR_YELLOW%!BROWSER_NAME!%COLOR_RESET%
echo %COLOR_WHITE%Port:%COLOR_RESET% %COLOR_YELLOW%%WEB_PORT%%COLOR_RESET%
echo %COLOR_WHITE%Host:%COLOR_RESET% %COLOR_YELLOW%%WEB_HOSTNAME%%COLOR_RESET%
echo %COLOR_WHITE%Access URL:%COLOR_RESET% %COLOR_BLUE%http://%WEB_HOSTNAME%:%WEB_PORT%%COLOR_RESET%
echo %COLOR_CYAN%========================================%COLOR_RESET%
echo.

:: ============================================
:: Clean Old Processes
:: ============================================
echo %COLOR_YELLOW%Cleaning old processes...%COLOR_RESET%
taskkill /F /IM dart.exe >nul 2>&1
taskkill /F /IM flutter.exe >nul 2>&1
timeout /t 1 /nobreak >nul
echo %COLOR_GREEN%✓ Process cleanup completed%COLOR_RESET%
echo.

:: ============================================
:: Check Flutter Environment
:: ============================================
where flutter >nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo %COLOR_RED%======================================%COLOR_RESET%
    echo %COLOR_BOLD%%COLOR_RED%ERROR: Flutter not found!%COLOR_RESET%
    echo %COLOR_RED%Please install Flutter and add to PATH%COLOR_RESET%
    echo %COLOR_RED%======================================%COLOR_RESET%
    echo.
    pause
    exit /b 1
)

echo %COLOR_GREEN%✓ Flutter environment OK%COLOR_RESET%
echo.

:: ============================================
:: Start Application
:: ============================================
echo %COLOR_CYAN%========================================%COLOR_RESET%
echo %COLOR_BOLD%%COLOR_GREEN%  Starting Flutter Web Application...%COLOR_RESET%
echo %COLOR_CYAN%========================================%COLOR_RESET%
echo.
echo %COLOR_YELLOW%Tips:%COLOR_RESET%
echo   %COLOR_GREEN%-%COLOR_RESET% Press %COLOR_CYAN%R%COLOR_RESET% to hot reload
echo   %COLOR_GREEN%-%COLOR_RESET% Press %COLOR_CYAN%Q%COLOR_RESET% to quit
echo   %COLOR_GREEN%-%COLOR_RESET% Press %COLOR_CYAN%Ctrl+C%COLOR_RESET% to force stop
echo.
echo %COLOR_CYAN%========================================%COLOR_RESET%
echo.

:: Start Flutter Web
flutter run -d %BROWSER% --web-port=%WEB_PORT% --web-hostname=%WEB_HOSTNAME%

:: ============================================
:: Exit Handling
:: ============================================
echo.
echo %COLOR_CYAN%========================================%COLOR_RESET%
echo %COLOR_YELLOW%  Application Stopped%COLOR_RESET%
echo %COLOR_CYAN%========================================%COLOR_RESET%
echo.
pause

